#include <QPainter>
#include <QStyleOption>
#include "tab-close-button.h"

TabCloseButton::TabCloseButton(QWidget *parent) :
    QToolButton(parent)
{
    setToolTip("Zamknij kartę");
}

void TabCloseButton::paintEvent (QPaintEvent* event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    QStyleOption option;
    option.init(this);
    option.state |= QStyle::State_AutoRaise;
    if (isEnabled() && underMouse() && !isChecked() && !isDown())
        option.state |= QStyle::State_Raised;
    if (isChecked())
        option.state |= QStyle::State_On;
    if (isDown())
        option.state |= QStyle::State_Sunken;

    if (const QTabBar* tabBar = qobject_cast<const QTabBar*>(parent()))
    {
        int index = tabBar->currentIndex();
        QTabBar::ButtonPosition position = (QTabBar::ButtonPosition)style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, tabBar);
        if (tabBar->tabButton(index, position) == this)
            option.state |= QStyle::State_Selected;
    }

    style()->drawPrimitive(QStyle::PE_IndicatorTabClose, &option, &painter, this);
}
